package com.Advance.Signle_;
//通过代码来了解单例设计模式
//以下为饿汉式的实现
public class signle01 {
    public static void main(String[] args) {
        Wife wife = Wife.getInstance();
        System.out.println(wife);
        System.out.println("====测试====");
        Wife wife02 = Wife.getInstance();
        System.out.println(wife02);
        System.out.println(wife==wife02);
    }
}
//主要的思想就是保证再整个系统中，对某个类中只能存在一个对象实例
//这里假如一个人只有一个妻子
class Wife{
    private String name ;
    private static Wife wife  = new Wife("小红") ;
    private Wife(String name){
        System.out.println("构造器被调用");
        this.name = name;
    }
    public static Wife getInstance(){
        return wife;
    }

    @Override
    public String toString() {
        return "Wife{" +
                "name='" + name + '\'' +
                '}';
    }
}
//弊端：有可能没有用到这个对象，但类的机制已经帮你将对象创建好了（创建好了，但没有使用，存在资源的浪费）

